package com.object.module.lq.sys.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.object.dao.sys.TNoticeMapper;
import com.object.module.lq.sys.entity.TNotice;
import com.object.module.lq.sys.entity.TUserEntity;
import com.object.module.lq.sys.service.TUserService;
import com.object.utils.MyQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.utils.Q;
import org.springframework.stereotype.Service;
import com.object.module.lq.sys.service.TNoticeService;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 通知
 **/
@Service
public class TNoticeServiceImpl extends ServiceImpl<TNoticeMapper, TNotice> implements TNoticeService
{
    @Resource
    private TUserService userService;

    @Override
    public Q listPage(Integer type)
    {
        LambdaQueryWrapper<TNotice> lqWrapper = new LambdaQueryWrapper<TNotice>()
        {{
            eq(!ObjectUtils.isEmpty(type), TNotice::getTnType, type);
            eq(TNotice::getTnUrId, StpUtil.getLoginId());
            orderByDesc(TNotice::getTnTime);
            orderByAsc(TNotice::getTnStats);
            orderByAsc(TNotice::getTnBehavior);
        }};
        List<TNotice> list = list(lqWrapper);
        list.forEach(i ->
                     {
                         i.setImgUrl(userService.getById(i.getTnUr()).getUrAvatar());
                     });
        return Q.ok().q("list", list);
    }

    @Override
    public Q<List<TNotice>> listPage(Map<String, Object> params)
    {
        MyQueryWrapper<TNotice> wrapper = new MyQueryWrapper<>(params, TNotice.class);
        QueryWrapper<TNotice> queryWrapper = wrapper.ConstructQuery();
        Q<List<TNotice>> q = wrapper.pageResponse(this, queryWrapper);
        List<TNotice> list =q.getT();
        if (Objects.isNull(list))
        {
            return Q.ok();
        }
        list.forEach(i ->
                     {
                         TUserEntity user = userService.getById(i.getTnUrId());
                         i.setImgUrl(user.getUrAvatar());
                         i.setNickName(user.getUrName());
                     });
        q.q("list", list);
        q.setT(list);
        return q;
    }
}
